package action.admin;

import bdd.ProduitBdd;
import service.securite.DroitsAcces;
import service.securite.Erreur;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import bean.produit.Produit;


public class AdminListeProduitAction extends Action {
	private static final long serialVersionUID = 8690425543984355314L;

	
	public static boolean aLesDroits(HttpServletRequest request)
	{
		if (DroitsAcces.isAdminProduit(request))
		{
			return true;
		}
		return false;
	}

	
	protected void initSession(HttpServletRequest request )
	{
		HttpSession session = request.getSession();
	}	
	
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest pRequete,
			final HttpServletResponse pReponse) {
		ActionErrors errors = new ActionErrors();
		this.initSession(pRequete);
		
		if (!aLesDroits(pRequete)) {
			errors.add("errors",new ActionMessage("errors.admin"));
			saveErrors(pRequete,errors); 
			return (new ActionForward(pMapping.getInput()));
		}
		int idService = (Integer) pRequete.getSession().getAttribute("id_service");
		
		try {
			Integer nbc = null;
			if (pRequete.getParameter("Categorie") != null)
			{
				nbc = new Integer(pRequete.getParameter("Categorie"));
			}
			System.out.println("categorie " + nbc);
			
			ArrayList<Produit> listP = ProduitBdd.getProduits(nbc,idService);				
			pRequete.setAttribute("listeproduits", listP);
			return pMapping.findForward("success");
			
		} catch (Exception e) {
			e.printStackTrace();
			errors.add("errors",new ActionMessage("errors.exception"));
			saveErrors(pRequete,errors); 
			return (new ActionForward(pMapping.getInput()));
		}
	}
}